package com.web.new12306.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.web.new12306.dao.OrderDaoByCaiyan;
import com.web.new12306.pojo.Order;


public class OrderDaoImplByCaiyan implements OrderDaoByCaiyan{
	Connection conn = null;
	PreparedStatement pstmt = null;
	Statement stmt = null;
	ResultSet rs = null;

	@Override
	public int number(Connection conn) {
		String sql="select count(*) from orders";
		int n=0;
		try {
			this.conn=conn;
			pstmt=conn.prepareStatement(sql);
			rs=pstmt.executeQuery();
			while(rs.next())
			{
				n=rs.getInt(1);
			}
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return n;
	}

	@Override
	public List<Order> select(Connection conn, Integer page, Integer rows) {
		String sql = "select * from orders limit ?,?";
		List<Order> list = new ArrayList<Order>();
		try {
			this.conn=conn;
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, (page-1)*rows);
			pstmt.setInt(2, page*rows);
			ResultSet rs = pstmt.executeQuery();
			while (rs.next()) {
				Order order=new Order();
				order.setTrainNumber(rs.getString("trainNumber"));
				order.setStartStation(rs.getString("startStation"));
				order.setTerminalStation(rs.getString("terminalStation"));
				order.setTime(rs.getTimestamp("time"));
				order.setPrice(rs.getInt("price"));
				list.add(order);

			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return list;
	}
}
